package org.springcloud.wisdom.amusement.login.service;

import org.springcloud.wisdom.amusement.login.dao.Clogindao;
import org.springcloud.wisdom.amusement.login.dao.Team_StudentDao2;
import org.springcloud.wisdom.amusement.login.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Cloginservice2 {
    @Autowired
    private Clogindao clogindao;

    @Autowired
    private Team_StudentDao2 teamStudentDao2;
    public Object login(LoginRequest loginRequest) {

        String username = loginRequest.getUsername();
        String password = loginRequest.getPassword();

        // 检查学生
        Student student = clogindao.login(username, password);
        if (student != null) {
            Team_Student teamStudent = teamStudentDao2.selectTeamStudent(student.getSid());
            if (teamStudent != null) {
                FullStudent fullStudent = new FullStudent();
                fullStudent.setSid(student.getSid());
                fullStudent.setIdentify(student.getIdentify());
                fullStudent.setSname(student.getSname());
                fullStudent.setGender(student.getGender());
                fullStudent.setAddress(student.getAddress());
                fullStudent.setSpassword(student.getAdpass());
                fullStudent.setPhone(student.getPhone());
                fullStudent.setSdept(student.getDept());
                fullStudent.setTalent(student.getTalent());
                String team = teamStudentDao2.selectTeamName(teamStudent.getTeamid());
                fullStudent.setTeam(team);
                fullStudent.setTeamid(teamStudent.getTeamid());
                String teamRole = null;
                if (teamStudent.getRole() == 0) {
                    teamRole = "成员";
                }
                if (teamStudent.getRole() == 1) {
                    teamRole = "社长";
                }
                if (teamStudent.getRole() == 2) {
                    teamRole = "外联部长";
                }
                fullStudent.setTeamRole(teamRole);
                return fullStudent;
            } else {
                return student;
            }
        }

        // 检查商家
        Mall mall = clogindao.login1(username, password);
        if (mall != null) {
            return mall;
        }

        // 检查管理员
        Admin admin = clogindao.login2(username, password);
        if (admin != null) {
            return admin;
        }

        //检查老师
        Teacher teacher = clogindao.login3(username, password);
        if (teacher != null) {
            return teacher;
        }

        //检查新生
        Freshman freshman = clogindao.login4(username, password);
        if (freshman != null) {
            return freshman;
        }

        //如果都不是
        return null;
    }
}
